package com.test.datebase;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class DataToSqlConverter {
    public static void main(String[] args) {
        String inputFilePath = "D:\\2(2).txt";
        String outputFilePath = "D:\\4-output_sql.sql";
        convertToSql(inputFilePath, outputFilePath);
    }

    public static void convertToSql(String inputFile, String outputFile) {
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile)); FileWriter writer = new FileWriter(outputFile)) {
            String line;
            boolean firstLine = true;
            writer.write("INSERT INTO tatp_prod.infra_file (id, config_id, path, url, type, size, creator, create_time, updater, update_time, deleted, secret_level, model_name, model_id, listfile_id, file_id, file_no, file_type, scope_userid, scope_class, package_no, contract_no, secret_year) VALUES\n");

            while ((line = reader.readLine()) != null) {
                if (!firstLine) {
                    writer.write(",\n");
                }
                firstLine = false;
                String[] fields = line.split("\t");
                for (int i = 0; i < fields.length; i++) {
                    if (fields[i].equals("\\N")) {
                        fields[i] = "NULL";
                    } else {
                        try {
                            Integer.parseInt(fields[i]);
                        } catch (NumberFormatException e) {
                            fields[i] = "'" + fields[i].replace("'", "''") + "'";
                        }
                    }
                }
                String joinedFields = String.join(", ", fields);
                writer.write("(" + joinedFields + ")");
            }
            writer.write(";\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}